<?php get_instance()->view('common/header');?>
<style>
    .content-btn-box,.content-box{margin-top:10px;}
    .container {
        width: 100%;
    }
    .add-tag-btn{line-height: 40px;}
    .option-item {
        margin-bottom: 5px;
        position: relative;
    }
    .del-tag-btn{font-size: 14px;line-height: 36px;display: block;margin-bottom: 4px;}
    .del-tag-btn a{color: red;}
    .add-img{margin-top: 9px;float: left;left: 0;}
    .radio-list{float: left;}
    .delete-btn{position: absolute;top: 0px;right: 64px;}
    .img-box .img{width: 200px;height: 130px}
    .webuploader-container > div:nth-child(2){width: 78px !important;height: 34px !important;}
    .webuploader-pick{background: #00b7ee !important;padding: 10px 15px !important;}
</style>
<div class="page-container home">
    <?php get_instance()->view('common/sidebar');?>
    <div class="page-content-wrapper">
        <div class="page-content">
            <div class="row">
                <div class="col-md-12">
                    <!-- BEGIN PAGE TITLE & BREADCRUMB-->
                    <h3 class="page-title"> <?php echo $title.' - 【位置：'.$position.'】';?></h3>
                    <ul class="page-breadcrumb breadcrumb">
                        <li class="btn-group"> <a class="btn default ml_5 return" href="<?php echo $backUrl?>"><i class="fa fa-chevron-left"></i> 返回</a> </li>
                        <li><i class="fa fa-home"></i><a href="<?php echo getUrl('config/ads');?>">广告管理 </a><i class="fa fa-angle-right"></i></li>
                        <li><a href="<?php echo getUrl('config/ads/home?type='.$type);?>">广告列表 </a><i class="fa fa-angle-right"></i></li>
                        <li><a href="javascript:void(0);"><?php echo $add ? '添加' : '编辑';?></a></li>
                    </ul>
                    <!-- END PAGE TITLE & BREADCRUMB-->
                </div>
            </div>
            <div class="row">
                <div class="col-md-12">
                    <div class="portlet box">
                        <div class="portlet-body form">
                            <form action="<?php echo getUrl('config/ads/save')?>" class="form-horizontal" method="post" id="main-form" novalidate="novalidate">
                                <div class="form-body">
                                    <div class="form-group">
                                        <label class="control-label col-md-2"> 广告图片<span class="required">*</span></label>
                                        <div class="col-md-2 img-box">
                                            <div class="upload-img line">
                                                <div class="img-box">
                                                    <img class="img images" src="<?php echo ! $add ? $info['pic'] : getDefaultImg();?>">
                                                    <input name="pic" type="hidden" class="img-input" value="<?php echo ! $add ? $info['pic'] : '';?>"/>
                                                </div>
                                                <div id="uploader-demo" style="margin-top:10px;margin-left: 10px;">
                                                    <div id="fileList" class="uploader-list"></div>
                                                    <div class="filePicker">选择图片</div>
                                                </div>
                                                <div style="width:100%;height:10px;">
                                                    <div id="progress" style="height:100%;width:0;background-color:green;">&nbsp;</div>
                                                </div>
                                            </div>
                                        </div>
                                        <script type="text/javascript">
                                            $(function () {
                                                var uploader = WebUploader.create({
                                                    auto: true,
                                                    server: '<?php echo getUrl('config/ads/uploadImge');?>', // 文件接收服务端
                                                    pick: '.filePicker',
                                                    // 只允许选择图片文件
                                                    accept: {
                                                        title: 'Images',
                                                        extensions: 'gif,jpg,jpeg,bmp,png',
                                                        mimeTypes: 'image/*'
                                                    },
                                                    fileVal:'Filedata'
                                                });

                                                //文件上传过程中创建进度条实时显示。
                                                uploader.on( 'uploadProgress', function( file, percentage ) {
                                                    $('#progress').css( 'width', percentage * 100 + '%' );
                                                });

                                                // 文件上传成功，给item添加成功class, 用样式标记上传成功。
                                                uploader.on( 'uploadSuccess', function( file, response) {
                                                    $('.images').attr('src', response._raw);
                                                    $('.img-input').val(response._raw);
                                                });

                                                // 完成上传完了，成功或者失败，先删除进度条。
                                                uploader.on( 'uploadComplete', function( file ) {
                                                    $('#progress').width(0);
                                                });
                                            });
                                        </script>
                                    </div>
                                    <div class="form-group">
                                        <label class="control-label col-md-2">广告名称<span class="required">*</span></label>
                                        <div class="col-md-7">
                                            <input class="form-control required" name="ads_name" type="text" value="<?php echo ! $add ? $info['ads_name'] : '';?>">
                                        </div>
                                    </div>
                                    <?php if (in_array($type,array(2,3,4,5))):?>
                                        <div class="form-group">
                                            <label class="control-label col-md-2">链接类型<span class="required">*</span></label>
                                            <div class="col-md-7" >
                                                <select class="form-control" name="type" data-placeholder="链接类型">
                                                    <option value="0">请选择</option>
                                                    <option value="1" <?php if (!$add && $info['type'] == 1):?> selected <?php endif;?> >外部链接</option>
                                                    <option value="2" <?php if (!$add && $info['type'] == 2):?> selected <?php endif;?> >商家</option>
<!--                                                    <option value="3" --><?php //if (!$add && $info['type'] == 3):?><!-- selected --><?php //endif;?><!-- >商品</option>-->
<!--                                                    <option value="4" --><?php //if (!$add && $info['type'] == 4):?><!-- selected --><?php //endif;?><!-- >分类</option>-->
                                                </select>
                                            </div>
                                        </div>
                                        <div class="form-group <?php if ($add || $info['type'] != 4):?> hide <?php endif;?> category">
                                            <label class="control-label col-md-2"> 链接分类<span class="required">*</span></label>
                                            <div class="col-md-7" >
                                                <select class="form-control" name="category_id" data-placeholder="请选择链接分类">
                                                    <option value="0">请选择</option>
                                                    <?php foreach ($category as $k => $v): ?>
                                                        <option value="<?php echo $v['id']?>" <?php if (!$add && $info['type'] == 4 && $info['param'] == $v['id']):?> selected <?php endif;?> ><?php echo $v['cat_name']?></option>
                                                    <?php endforeach;?>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="form-group <?php if ($add || in_array($info['type'],array(0,1,4))):?> hide <?php endif;?> merchant">
                                            <label class="control-label col-md-2"><?php if (!$add && $info['type'] == 2):?>链接<?php endif;?>商家<span class="required">*</span></label>
                                            <div class="col-md-7">
                                                <input class="form-control required" name="merchant_id" type="hidden" value="<?php echo !$add && ($info['type'] == 2 || $info['type'] == 3) ? $info['merchant_id'] : ''?>">
                                            </div>
                                        </div>
                                        <script type="text/javascript">
                                            var merchantId = 0;
                                            $(function(){
                                                //select2 搜索商家
                                                $('input[name="merchant_id"]').select2({
                                                    closeOnSelect: true,
                                                    placeholder: '搜索商家,输入店铺名称、电话',
                                                    minimumInputLength: 2,
                                                    ajax: {
                                                        url: "<?php echo getUrl('common/searchUserName')?>",
                                                        dataType: 'json',
                                                        data: function (term, page) {
                                                            return {
                                                                keyword: term
                                                            };
                                                        },
                                                        results: function (data, page) {
                                                            return {
                                                                results: data
                                                            };
                                                        }
                                                    },
                                                    formatResult: function(movie) {
                                                        return movie.name;
                                                    },
                                                    formatSelection: function(movie) {
                                                        return movie.name;
                                                    },
                                                    dropdownCssClass: "bigdrop",
                                                    escapeMarkup: function (m) {
                                                        return m;
                                                    }
                                                }).change(function (evt) {
                                                    merchantId = evt.added.id;
                                                    $('.select2-input').removeAttr('disabled');
                                                    $('.product').find('.select2-chosen').html('搜索'+evt.added.name+'店铺下的商品,输入商品名称');
                                                });
                                            });
                                        </script>

                                        <div class="form-group <?php if ($add || $info['type'] != 3):?> hide <?php endif;?> product">
                                            <label class="control-label col-md-2">链接商品<span class="required">*</span></label>
                                            <div class="col-md-7">
                                                <input class="form-control required" name="product_id" type="hidden" value="<?php echo !$add && $info['type'] == 3 ? $info['param'] : ''?>">
                                            </div>
                                        </div>
                                        <script type="text/javascript">
                                            $(function(){
                                                //select2 搜索商家
                                                $('input[name="product_id"]').select2({
                                                    closeOnSelect: true,
                                                    placeholder: '先选择商家,再选择商品',
                                                    minimumInputLength: 2,
                                                    ajax: {
                                                        url: "<?php echo getUrl('common/searchProductName')?>",
                                                        dataType: 'json',
                                                        data: function (term, page) {
                                                            return {
                                                                keyword: term,
                                                                merchantId:merchantId
                                                            };
                                                        },
                                                        results: function (data, page) {
                                                            return {
                                                                results: data
                                                            };
                                                        }
                                                    },
                                                    formatResult: function(movie) {
                                                        return movie.name;
                                                    },
                                                    formatSelection: function(movie) {
                                                        return movie.name;
                                                    },
                                                    dropdownCssClass: "bigdrop",
                                                    escapeMarkup: function (m) {
                                                        return m;
                                                    }
                                                });
                                                // 设置商家默认显示
                                                <?php if (!$add && $info['merchant_id'] > 0):?>
                                                merchantId = "<?php echo $info['merchant_id']?>";
                                                var name = "<?php echo $mInfo['shop_name']?>";
                                                $('.product').find('.select2-chosen').html('搜索'+name+'店铺下的商品,输入商品名称');
                                                $('input[name="merchant_id"]').parents('.col-md-7')
                                                    .find('.select2-chosen')
                                                    .text('<?php echo $mInfo['shop_name']?>')
                                                    .parent('a')
                                                    .removeClass('select2-default');
                                                <?php endif;?>

                                                // 设置商品默认显示
                                                <?php if (!$add && $info['type'] == 3 && $info['param'] > 0):?>
                                                $('input[name="product_id"]').parents('.col-md-7')
                                                    .find('.select2-chosen')
                                                    .text('<?php echo $param['name']?>')
                                                    .parent('a')
                                                    .removeClass('select2-default');
                                                <?php elseif(!$add && $info['type'] == 4):?>
                                                $('.product').find('.select2-input').attr('disabled', 'disabled');
                                                <?php endif;?>
                                            });
                                        </script>
                                        <div class="form-group <?php if ($add || $info['type'] != 1):?> hide <?php endif;?> url">
                                            <label class="control-label col-md-2">外链<span class="required">*</span></label>
                                            <div class="col-md-7">
                                                <input class="form-control " name="url" type="text" value="<?php echo ! $add ? $info['url'] : '';?>">
                                            </div>
                                        </div>
                                    <?php endif;?>
                                    <!-- 按钮 -->
                                    <div class="form-actionsc fluid">
                                        <div class="col-md-offset-2 col-md-9" style="padding-left: 0;">
                                            <input name="id" value="<?php echo ! $add ? $info['id'] : '';?>" type="hidden">
                                            <input name="ads_position" value="<?php echo $type;?>" type="hidden">
                                            <input type="hidden" value="<?php echo $backUrl?>" name="backUrl">
                                            <button class="btn green"><i class="fa fa-save"></i> 保存</button>
                                            <a class="btn default seach-btn" href="<?php echo $backUrl;?>"><i class="fa fa-chevron-left"></i> 返回</a>
                                        </div>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(function(){
        $.e_validate.init('#main-form', {});

        $('[dp]').not("[noinit]").datepicker({
            changeMonth: true,
            autoclose: true,
            format: "yyyy-mm-dd",
            language: 'zh-CN'
        });

        $("[name='ads_position']").change(function () {
            var vl = $("[name='ads_position']").val();
            var merchant = $("[name='merchant']").val();
            if(vl == 2 || vl == 3 || vl == 4 || vl == 5 || merchant > 0){
                $("[name='url']").parents('.form-group').addClass('hide');
            }else{
                $("[name='url']").parents('.form-group').removeClass('hide');
            }
        })

        $("[name='type']").change(function () {
            var vl = $(this).val();
            if (vl == 1){
                $(".url").removeClass('hide');
                $(".category").addClass('hide');
                $(".merchant").addClass('hide');
                $(".product").addClass('hide');
            }else if (vl == 2){
                $(".merchant").removeClass('hide');
                $(".merchant").find('label').html('链接商家<span class="required">*</span>');
                $(".category").addClass('hide');
                $(".product").addClass('hide');
                $(".url").addClass('hide');
            }else if(vl == 3){
                $(".merchant").removeClass('hide');
                $(".merchant").find('label').html('商家<span class="required">*</span>');
                $(".product").removeClass('hide');
                $(".category").addClass('hide');
                $(".url").addClass('hide');
            }else if(vl == 4){
                $(".category").removeClass('hide');
                $(".merchant").addClass('hide');
                $(".product").addClass('hide');
                $(".url").addClass('hide');
            }else{
                $(".category").addClass('hide');
                $(".merchant").addClass('hide');
                $(".product").addClass('hide');
                $(".url").addClass('hide');
            }
        });
    });

    $(function(){
        setTimeout("$('.form-actions').width($('.form-body').innerWidth()).removeClass('hide');", '500');
        window.onresize = function(){
            $('.form-actions').width($('.form-body').innerWidth());
        }
    });
</script>
</body>
</html>